<?php
    class userprofileclass{
        public $id;
        public $userid;
        public $gender;
        public $dob;
        public $job;
        public $town;
        public $profilepic;

        function initwithuserid($userid){
            require 'phpincludes/dbconnect.php';
            $query = "SELECT * FROM `".$tbluserprofiles."` WHERE `userid` = '".$userid."'";
            $res = mysql_query($query);
            $row = mysql_fetch_assoc($res);
            mysql_close();

            // Load profile of user or load userid into empty object
            if($row['id']){
                foreach($row as $key => $val){
                    $this->$key = $val;
                }
            }
            else
                $this->userid = $userid;
        }
        
        function getgender(){
            global $male, $female;
            if($this->gender == 'm')
                return $male;
            elseif($this->gender == 'f')
                return $female;
            else
                return '';
        }
        
        function getdob(){
            if($this->dob != '0000-00-00')
                return $this->dob;
            else
                return '';
        }

        function getdobday(){
            if($this->dob != '0000-00-00')
                return substr($this->dob, 8, 2);
            else
                return '';
        }

        function getdobmonth(){
            if($this->dob != '0000-00-00')
                return substr($this->dob, 5, 2);
            else
                return '';
        }

        function getdobyear(){
            if($this->dob != '0000-00-00')
                return substr($this->dob, 0, 4);
            else
                return '';
        }
        
        function setgender($gender){
            if(($gender == 'm') || ($gender == 'f'))
                $this->gender = $gender;
        }

        function setdob($dobday, $dobmonth, $dobyear){
            if((1 <= $dobday && $dobday <= 31) && (1 <= $dobmonth && $dobmonth <= 12) && (1900 <= $dobyear && $dobyear <= date('Y')))
                $this->dob = $dobyear.'-'.$dobmonth.'-'.$dobday;
        }

        function saveself(){
            $dbinfo = array('userid' => stripslashes($this->userid),
                            'gender' => stripslashes($this->gender),
                            'dob' => stripslashes($this->dob),
                            'job' => stripslashes($this->job),
                            'town' => stripslashes($this->town),
                            'profilepic' => stripslashes($this->profilepic));

            require 'phpincludes/dbconnect.php';

            foreach ($dbinfo as $column => $value)
                $dbinfo[$column] = mysql_real_escape_string($value);

            if($this->id > 0){
                // Profile exists, update using "col1=val1,col2=val2" string
                $i = 0;
                $setpieces = array();
                foreach ($dbinfo as $column => $value){
                    $setpieces[$i] = "`".$column."` = '".$value."'";
                    $i++;
                }
                $sets = implode(", ", $setpieces);

                $query = "UPDATE `".$tbluserprofiles."` SET ".$sets." WHERE `id` = '".$this->id."'";
            }
            else{
                // Profile does not exist, insert using "(col1,col2) VALUES (val1,val2)" string
                $columns = implode("`, `", array_keys($dbinfo));
                $values = implode("', '", $dbinfo);

                $query = "INSERT INTO `".$tbluserprofiles."` (`".$columns."`) VALUES ('".$values."')";
            }
            
            $res = mysql_query($query);
            mysql_close();
            return $res;
        }

        function dumpself(){
            echo '<br/>';
            echo 'User profile object<br/>';
            echo '-------------------<br/>';
            echo 'id -> '.$this->id.'<br/>';
            echo 'userid -> '.$this->userid.'<br/>';
            echo 'gender -> '.$this->gender.'<br/>';
            echo 'dob -> '.$this->dob.'<br/>';
            echo 'job -> '.$this->job.'<br/>';
            echo 'town -> '.$this->town.'<br/>';
            echo 'profilepic -> '.$this->profilepic.'<br/>';
        }
    }

?>
